package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Task;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class TaskService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieves the session (connection).
     */
    public TaskService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds Task to the db.
     * @param task Task to be inserted
     * @return ID of the added Task
     */
    public int addTask(Task task) {
        session.beginTransaction();
        int id = getMaxTaskID() + 1;
        task.setTaskID(id);
        id = (Integer) session.save(task);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of Tasks.
     * @return Max ID
     */
    public int getMaxTaskID() {
        String hql = "select max(t.taskID) from Task t";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxtaskid = (Integer) list.get(0);
            return maxtaskid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a Task identified by the ID.
     * @param id of the Task
     * @return Task with the ID given (null if not found)
     */
    public Task getTaskByID(int id) {
        session.beginTransaction();
        Task task = (Task) session.get(Task.class, new Integer(id));
        session.getTransaction().commit();
        return (task);
    }

    /**
     * Retrieves a list of all Tasks stored in the db.
     * @return List of all Tasks (null if no Tasks)
     */
    public List<Task> getAllTasks() {
        session.beginTransaction();
        List<Task> taskList = session.createQuery("from Task").list();
        session.getTransaction().commit();
        return (taskList);
    }

    /**
     * Updates a specified Task identified by ID.
     * @param newTask an updated Task
     */
    public void updateTask(Task newTask) {
        session.beginTransaction();
        Task task = (Task) session.get(Task.class, new Integer(newTask.getTaskID()));
        task.setDescription(newTask.getDescription());
        task.setDueTo(newTask.getDueTo());
        task.setEmployee(newTask.getEmployee());
        task.setState(newTask.getState());
        task.setSupplier(newTask.getSupplier());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Task from db.
     * @param id Task ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteTask(int id) {
        try {
            session.beginTransaction();
            Task task = (Task) session.get(Task.class, new Integer(id));
            session.delete(task);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}